package com.teacold.customdemo.chapter_seven;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class LinearGradientView extends View {
    private Paint mPaint;

    public LinearGradientView(Context context) {
        this(context,null);
    }

    public LinearGradientView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LinearGradientView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint=new Paint();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mPaint.setShader(new LinearGradient(0,getHeight()/2,getWidth(),getHeight()/2,0xffff000,0xff00ff00,Shader.TileMode.CLAMP));
//        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
        LinearGradient(canvas);
    }

   private void  LinearGradient(Canvas canvas){
        int[] colors={0xfff0000,0xff00ff00,0xff0000ff,0xffffff00,0xff00ffff};
        float[] pos={0f,0.2f,0.4f,0.6f,1.0f};
        LinearGradient multiGradient=new LinearGradient(0,0,getWidth()/2,getHeight()/2,colors,pos,Shader.TileMode.CLAMP);
        mPaint.setShader(multiGradient);
//        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
         mPaint.setTextSize(50);
         canvas.drawText("欢迎关注旗舰的blog",0,200,mPaint);

   }


}
